# coding=utf-8
import pymongo
from pymongo import  MongoClient
import redis
from TikuTypeDict import  *
from PaperTypeDict import *
import json;
from tiRedisKey import  TiRedisKey

from tk.redisoper import  CRedis
from bson.objectid import ObjectId
from elasticsearch import  Elasticsearch
from Pagination import  Pagination
from tiES import TiES
ties=TiES();

class Tidao :
    def __init__(self):
        #self.client=MongoClient('mongodb://localhost:27010/db')
        self.client=MongoClient('47.92.139.146',27017)
        self.db=self.client['tiku']
        self.db.authenticate('tiku','tiku123')
        self.redis=CRedis()
    #新增学科类型和题型关系


    def addSubjectTypeRel(self):
        subject_type_rel=self.db.subject_type_rel
        subject=[
            {'id':'1','subject':'语文','types':[{'id':'1','desc':'单选题'}]},
            {'id':'2','subject':'数学','types':[{'id':'1','desc':'单选题'}]}
        ]
        self.redis.set('subject_type_rel')
        subject_type_rel.insert(subject);
    #获取学科类型和题型关系
    def getSubjectTypeRel(self):
        v=[SUB_YW,SUB_SX,SUB_YY,SUB_WL,SUB_HX,SUB_LS,SUB_ZZSP,SUB_DL,SUB_SW];
        return v;

    #获取试卷类型查询选项
    def getPaperTypeRel(self):
        v={"chuzhong":GRADE_CHUZHONG,"gaozhong":GRADE_GAOZHONG,"channel":CHANNEL,
           "year":YEAR,"area":PAREA,"ptype":PTYPE};
        return v;


    #获取左侧推荐试题列表
    def getLeftRecommend(self,subject, type):
        paper=self.db['shiji_paper'];
        vs = paper.find({},{"_id":0}).limit(20);
        values=[ v for v in vs]
        return values;
    #获取题目内容详情
    def getTiDetail(self,id):
        v=self.getTiDetailFromRedis(id)
        if v is None:
            v=self.getTiDetailFromMongo(id)
        return v;
    #获取题目内容详情(取redis)
    def getTiDetailFromRedis(self,id):
        key=TiRedisKey.TI_DETAIL%(id);
        v=self.redis.get(key);
        if(v is not None):
            value=json.loads(v);
            return value;
        return None;
    #获取题目内容详情(取mongodb)
    def getTiDetailFromMongo(self,id):
        key = TiRedisKey.TI_DETAIL % (id);
        tiku=self.db['shiji211'];
        v= tiku.find({"id":id},{"_id":0}).limit(1);
        if(v is not None):
            self.redis.set(key,json.dumps(v[0]))
        return v[0];
    #获取题目title
    def getTiTitle(self,id):
        v=self.getTiTitleFromRedis(id);
        if v is None:
            v=self.getTiTileFromMondb(id);
        return v ;

    def getpaperdetail(self,pid):
        tiku = self.db['shiji211'];
        vs=tiku.find({"blpaper":pid},{"_id":0});
        values=[v for v in vs]
        return values;



    #从redis获取题目标题
    def getTiTitleFromRedis(self,id):
        key=TiRedisKey.TI_TITLE%(id);
        v=self.redis.get(key);
        return v;
    #从mondob获取题目标题
    def getTiTileFromMondb(self,id):
        tiku = self.db.tiku;
        tid = TiRedisKey.TI_DETAIL % (id);
        v = tiku.find_one({'id': id},{"_id":0})
        return v.title;

    # 获取题目列表(分页)
    def getTisPage(self,keyword='',channel='',grade='',paperyear='',paperarea='',papertype='',pageno=1,pagesize=10):
        timus = [];
        vs = ties.searchTi(keyword,channel,grade,paperyear,paperarea,papertype, pageno, pagesize);
        if len(vs['hits']['hits']) == 0:
            return;
        length = vs['hits']['total'];
        for hit in vs['hits']['hits']:
            id = hit['_source']['id']
            timus.append(self.getTiDetail(id))
        page = Pagination(timus, length);
        return page;

    def getPaperPage(self,keyword='',channel='',grade='',paperyear='',paperarea='',papertype='',pageno=1,pagesize=10):
        papers = [];
        length = 0;
        vs = ties.searchTiByKeyWord(keyword, channel, grade, paperyear, paperarea,papertype, pageno, pagesize);
        if len(vs['hits']['hits']) == 0:
            return;
        length = vs['hits']['total'];
        for hit in vs['hits']['hits']:
            id = hit['_source']['id']
            papers.append(self.getTiDetail(id))
        page = Pagination(papers, length);
        return page;



    #根据关键字搜索题目列表
    def searchTi(self,keyword='',channel='',grade='',paperyear='',paperarea='',ptype='',pageno=1,pagesize=10):
        timus=[];
        length=0;
        vs=ties.searchTiByKeyWord(keyword,channel,grade,paperyear,paperarea,pageno,pagesize);
        if len(vs['hits']['hits'])==0:
            return;
        length=vs['hits']['total'];
        for hit in vs['hits']['hits']:
            id=hit['_source']['id']
            timus.append(self.getTiDetail(id))
        page=Pagination(timus,length);
        return page;
    #获取相关试题
    def simlarTimus(self,id):
        paper = self.db['shiji_paper'];
        vs = paper.find({},{"_id":0}).limit(20);
        values = [v for v in vs]
        return values;


















